package com.ibeeking.found.common.utils;

import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils extends DateUtil {
    public static String YYYYMMDD = "yyyyMMdd";
    public static String HH_MM = "HH:mm";
    public static String YEAR_DAY_FIRST = "-01-01";
    public static String DAY_START = " 00:00:00";
    public static String DAY_END = " 23:59:59";
    public static Integer TEN = 10;
    /**
     * 一天的毫秒值
     */
    public static Integer ONE_DAY_MILLIS = 86400000;
    public static Integer ONE_YEAR_MILLIS = ONE_DAY_MILLIS * 365;


    public static String localTimeToStr(LocalTime localTime, String format) {
        return localTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static LocalTime strToLocalTime(String time, String format) {
        try {
            return LocalTime.parse(time, DateTimeFormatter.ofPattern(format));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Integer getCurrentYearInt() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    /**
     * 获取到当前年的第一天
     *
     * @return
     */
    public static String getCurrentYearFirstDay() {
        return getCurrentYearInt() + YEAR_DAY_FIRST;
    }

    /**
     * 获取到当前年的第一天 精确到秒
     *
     * @return
     */
    public static String getCurrentYearFirstDayTime() {
        return getCurrentYearInt() + YEAR_DAY_FIRST + DAY_START;
    }

    /**
     * 给2020-01-01改为  2020-01-01 00:00:00
     *
     * @param dayStr
     * @return
     */
    public static String dayStartStr(String dayStr) {
        if (StringUtils.isBlank(dayStr) || dayStr.length() != TEN) {
            return dayStr;
        }
        return dayStr + DAY_START;
    }

    /**
     * 例子 给2020-01-01改为  2020-01-01 23:59:59
     *
     * @param dayStr
     * @return
     */
    public static String dayEndStr(String dayStr) {
        if (StringUtils.isBlank(dayStr) || dayStr.length() != TEN) {
            return dayStr;
        }
        return dayStr + DAY_END;
    }

    /**
     * 获取time的毫秒值
     *
     * @param time
     * @param formart
     * @return
     */
    public static Long getTimeMillis(String time, String formart) {
        if (StringUtils.isAnyBlank(time, formart)) {
            return null;
        }
        return parseDate(time, formart).getTime();
    }

    /**
     * 获取今天 YYYY_MM_DD
     *
     * @return
     */
    public static String getToday() {
        return formatToStr(Calendar.getInstance().getTime(), YYYY_MM_DD);
    }

    /**
     * 获取昨天 YYYY_MM_DD
     *
     * @return
     */
    public static String getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        return formatToStr(calendar.getTime(), YYYY_MM_DD);
    }

    /**
     * 时间格式校验,格式不正确抛出异常
     *
     * @return
     */
    public static Date checkTimeStr(String time, String formart, String errorMsg) {
        errorMsg = StringUtils.isBlank(errorMsg) ? "时间格式不正确!" : errorMsg;
        if (StringUtils.isAnyBlank(time, formart)) {
            throw BusinessException.create(errorMsg);
        }
        SimpleDateFormat formatter = new SimpleDateFormat(formart);
        try {
            return formatter.parse(time);
        } catch (Exception e) {
            throw BusinessException.create(errorMsg);
        }
    }


}
